package kr.hyunist.androidcomplete01saeildamil.ch19;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.content.DialogInterface.OnClickListener;
import android.os.*;
import android.view.*;
import android.widget.*;

public class LongTimeEx05 extends Activity {
	int mValue;
	TextView mText;
	ProgressDialog mProgress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.longtimeex);

		mText = (TextView) findViewById(R.id.longtimetv);
	}

	public void mOnClick(View v) {
		new AccumulateTask().execute(100);
	}

	class AccumulateTask extends AsyncTask<Integer, Integer, Integer> {

		@SuppressWarnings("deprecation")
		@Override
		protected void onPreExecute() {
			mValue = 0;
			mProgress = new ProgressDialog(LongTimeEx05.this);
			mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgress.setTitle("Updating");
			mProgress.setMessage("Wait..");
			mProgress.setCancelable(false);
			mProgress.setProgress(0);
			mProgress.setButton("Cancel", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					cancel(true);
				}
				
			});
			mProgress.show();
		}

		@Override
		protected Integer doInBackground(Integer... arg0) {
			while(isCancelled() == false){
				mValue++;
				if(mValue <= 100){
					publishProgress(mValue);
				} else {
					break;
				}
				
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			return mValue;
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			mProgress.setProgress(values[0]);
			mText.setText(Integer.toString(values[0]));
		}

		@Override
		protected void onPostExecute(Integer result) {
			mProgress.dismiss();
			Toast.makeText(LongTimeEx05.this, "업로드 완료!", 0).show();
		}

		@Override
		protected void onCancelled() {
			mProgress.dismiss();
			Toast.makeText(LongTimeEx05.this, "사용자에 의한 취소!", 0).show();
		}

	}
}
